#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : mike
## date   : 2014.12.4
"""
"""
import os 
import sys
from os.path import realpath, dirname, basename, isdir

sys.path.append(dirname(dirname(realpath(__file__))))
from xbc3.fileio import FileIO

class CpvToUrl(object):
    
    def __init__(self, *inputfiles):
        super(CpvToUrl, self).__init__()
        self.ss_files_name = []
        self.cpv_files_name = []
        for fn in inputfiles:
            if isdir(fn):
                continue
            if 'sys_ss' in basename(fn):
                self.ss_files_name.append(fn)
            if 'sys_bf_cpv' in  basename(fn):
                self.cpv_files_name.append(fn)
        if not self.cpv_files_name :
            print('Error:can not find files!!!\n', file=sys.stderr)
            sys.exit()
        self.cookie_set=set()
        self.referer_dict={}
            
    def get_cpv_by_check(self, filename):
        print('get_cpv_by_check',filename)
        file = FileIO(filename)
        for line in file:  
            try:
                line=line.rstrip('\r\n')
                cpv=int(line[line.find('|cpv:')+5:line.find('|cpm:')])
                if cpv==1 or cpv==2 or cpv==3:
                    self.cookie_set.add(line[44:76])
                        
            except Exception as e:
                pass
    
    def get_url(self, filename):
        print('get_url',filename)
        file = FileIO(filename)
        for line in file:  
            try:
                line=line.rstrip('\r\n')
                cookie=line[44:76]
                if cookie in self.cookie_set:
                    referer = line[line.find('referer:')+8:line.find('|l:')]
                    referer = referer.split('?')[0]
                    if referer in self.referer_dict:
                        self.referer_dict[referer]+=1
                    else:
                        self.referer_dict[referer]=1
                        
            except Exception as e:
                pass
    
    def output(self):
        """"""
        fpath=os.path.split(self.ss_files_name[0])[0]
        result_path=os.path.join(fpath,'result.csv')
        
        with FileIO(result_path,'w') as f:
            f.write('referer,times\n')
            for k in sorted(self.referer_dict, key=lambda x:self.referer_dict[x],reverse=True):
                v = self.referer_dict[k]
                f.write('%s, %d\n' % (k, v))
    
    def run(self):
        """"""
        for fn in self.cpv_files_name:
            self.get_cpv_by_check(fn)
        for fn in self.ss_files_name:
            self.get_url(fn)   
        
        self.output()
    
if __name__ == '__main__':
    '''driver'''
    inputfiles = []
    for line in sys.stdin:
        line = line.rstrip('\r\n')
        inputfiles.append(line)
    ctu=CpvToUrl(*inputfiles)
    ctu.run()
         
    